import 'dart:collection';
import 'package:common_utils/common_utils.dart';

import 'package:eq/src/core.dart';
import 'package:eq/src/interface.dart';
import 'package:eq/src/interpreter.dart';
import 'package:eq/src/utils.dart';

StatementResult functionCallbyName(
    String module, String funName, List<Value> args,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  FunctionDefinition? functionDefinition = searchFunction(module, funName);
  if (functionDefinition == null) {
    result.type = StatementResultType.ERROR_RUNTIME_RESULT;
    result.error = "$module.$funName:函数未找到";
    return result;
  }
  if (functionDefinition.isNative) {
    result.returnvalue = functionDefinition.proc(args);
  } else {
    LinkedList<Variable> mlocalEnvironment = LinkedList(); //申请本地变量

    ///根据参数数量判断
    if (functionDefinition.parameter.isNotEmpty) {
      int i = 0;
      for (var entry in functionDefinition.parameter) {
        if (i >= args.length) {
          mlocalEnvironment.add(Variable(
              entry.name, Value(ValueType.UNDEFINED_VALUE, 'undefined')));
        } else {
          mlocalEnvironment.add(Variable(entry.name, args[i++]));
        }
      }
    }

    // if (args.isEmpty || functionDefinition.parameter.isEmpty) {
    //   ///只要有一个是空的就不需要赋值了
    // } else {
    //   Parameter? param = functionDefinition.parameter.first;
    //   for (int i = 0;
    //       (i < args.length) && (param != null);
    //       param = param.next, i++) {
    //     _localEnvironment.add(Variable(param.name, args[i], isFixed: false));
    //   }
    // }
    for (var t1 in functionDefinition.block.statementList) {
      if (t1.type == StatementType.CONTINUE_STATEMENT ||
          t1.type == StatementType.BREAK_STATEMENT) {
        t1.unlink();
      }
    }

    result = interpreterRun(module, functionDefinition.block.statementList,
        localEnvironment: mlocalEnvironment);

    if (result.type != StatementResultType.ERROR_RUNTIME_RESULT) {
      result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
    }
  }
  return result;
}

///module:模块调用方
StatementResult functionCallExpression(LinkedList<Variable>? localEnvironment,
    String module, Expression expressions) {
  StatementResult result = StatementResult();

  ///1.保存标识符
  ///2.搜索本地环境
  ///3.搜索全局环境，有bug 就是无法识别带$的变量 已解决
  String? package = expressions.functionCallExpression.function.module;
  if (package != null) {
    ///表示是其它模块的函数
    if (!Interpreter.getInstance().module[module]!.imp.containsKey(package)) {
      result.type = StatementResultType.ERROR_RUNTIME_RESULT;
      result.error = "$package 模块未导入";
      return result;
    }
    package = Interpreter.getInstance().module[module]!.imp[package]!;
    if (!Interpreter.getInstance().module.containsKey(package)) {
      ///模块还未编译
      loadModule(package);
    }
  }
  String funName = expressions.functionCallExpression.function.identifier;

  Variable? v;

  ///先处理标识符,这里只对带有$好的全局标识符做解析,
  ///解析时先找局部再找全局
  if (expressions.functionCallExpression.function.type ==
      ExpressionType.GLOBAL_EXPRESSION) {
    if (localEnvironment == null) {
      v = searchGlobalVariable(package ?? module, funName);
    } else {
      v = searchLocalVariable(localEnvironment, funName) ??
          searchGlobalVariable(package ?? module, funName);
    }
    if (v != null) {
      if (v.value.type != ValueType.FUNCTION_VALUE) {
        result.type = StatementResultType.ERROR_RUNTIME_RESULT;
        result.error = "$funName 值类型必须为函数类型";
        return result;
      } else {
        ///针对赋值函数指针
        package = Interpreter.getInstance().module[module]!.imp[v.value.module];
        funName = v.value.value;
      }
    } else {
//      result.returnvalue = Value(ValueType.UNDEFINED_VALUE, "undefined");
      result.type = StatementResultType.ERROR_RUNTIME_RESULT;
      result.error = "$funName 值未定义";
      return result;
    }
  }

  FunctionDefinition? functionDefinition =
      searchFunction(package ?? module, funName);
  if (functionDefinition == null) {
    result.type = StatementResultType.ERROR_RUNTIME_RESULT;
    result.error =
        "[${expressions.rowNum}:${expressions.colNum}]$funName:函数未找到";
    return result;
  }
  if (functionDefinition.isNative) {
    List<Value> args = List.empty(growable: true);

    // if ((functionDefinition.count != -1) &&
    //     (expressions.functionCallExpression.argument.length !=
    //         functionDefinition.count)) {
    //   result.type = StatementResultType.ERROR_RUNTIME_RESULT;
    //   result.error =
    //       "[${expressions.rowNum}:${expressions.colNum}]:$funName:参数不匹配";
    //   return result;
    // }
    for (var t1 in expressions.functionCallExpression.argument) {
      result = expressionStatement(module, t1.expression,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
        return result;
      }
      args.add(result.returnvalue);
    }
    result.returnvalue = functionDefinition.proc(args);
  } else {
    LinkedList<Variable> mlocalEnvironment = LinkedList(); //申请本地变量
    ///不做参数不匹配了,直接以参数最少的作为基准赋值,简单实现多态函数
    // if (expressions.functionCallExpression.argument.length !=
    //     functionDefinition.parameter.length) {
    //   result.type = StatementResultType.ERROR_RUNTIME_RESULT;
    //   result.error = "[${expressions.rowNum}:${expressions.colNum}]"
    //       ":${expressions.functionCallExpression.function.identifier}参数不匹配";
    //   return result;
    // }

    if (functionDefinition.parameter.isNotEmpty) {
      ArgumentList? arg = expressions.functionCallExpression.argument.isEmpty
          ? null
          : expressions.functionCallExpression.argument.first;
      for (var entry in functionDefinition.parameter) {
        if (arg != null) {
          result = expressionStatement(module, arg.expression,
              localEnvironment: localEnvironment);
          if (result.type != StatementResultType.NORMAL_STATEMENT_RESULT) {
            return result;
          }
          mlocalEnvironment.add(Variable(entry.name, result.returnvalue));
        } else {
          mlocalEnvironment.add(Variable(
            entry.name,
            Value(ValueType.UNDEFINED_VALUE, 'undefined'),
          ));
        }
        arg = arg?.next;
      }
    }

    for (var t1 in functionDefinition.block.statementList) {
      if (t1.type == StatementType.CONTINUE_STATEMENT ||
          t1.type == StatementType.BREAK_STATEMENT) {
        t1.unlink();
      }
    }

    result = interpreterRun(
        package ?? module, functionDefinition.block.statementList,
        localEnvironment: mlocalEnvironment);

    if (result.type != StatementResultType.ERROR_RUNTIME_RESULT) {
      result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
    }
  }
  return result;
}

StatementResult expressionStatement(String module, Expression expressions,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  Variable? v;

  switch (expressions.type) {
    case ExpressionType.BOOLEAN_EXPRESSION:
      result.returnvalue =
          Value(ValueType.BOOLEAN_VALUE, expressions.boolvalue);
      break;
    case ExpressionType.INT_EXPRESSION:
      result.returnvalue = Value(ValueType.INT_VALUE, expressions.intvalue);
      break;
    case ExpressionType.DOUBLE_EXPRESSION:
      result.returnvalue =
          Value(ValueType.DOUBLE_VALUE, expressions.doublevalue);
      break;
    case ExpressionType.STRING_EXPRESSION:
      result.returnvalue =
          Value(ValueType.STRING_VALUE, expressions.stringvalue);
      break;
    case ExpressionType.REGEXP_EXPRESSION:
      result.returnvalue =
          Value(ValueType.REGEXP_VALUE, expressions.stringvalue);
      break;
    case ExpressionType.IDENTIFIER_EXPRESSION:

      ///表示有本地变量环境,因为本地变量访问全局变量都需要使用$符号
      if (localEnvironment != null && expressions.module == null) {
        v = searchLocalVariable(localEnvironment, expressions.identifier);
      } else {
        v = searchGlobalVariable(module, expressions.identifier);
      }
      if (v == null) {
        ///都没搜到,试试是不是函数名
        FunctionDefinition? t1 = searchFunction(module, expressions.identifier);
        if (t1 != null) {
          result.returnvalue = Value(ValueType.FUNCTION_VALUE, t1.name,
              module: expressions.module ?? module)
            ..path = module;
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error =
              "[${expressions.rowNum}:${expressions.colNum}]:${expressions.identifier}标志未定义";
          // result.returnvalue = Value(ValueType.UNDEFINED_VALUE, "undefined");
        }
      } else {
        result.returnvalue = v.value;
      }
      break;
    case ExpressionType.GLOBAL_EXPRESSION:
      v = searchGlobalVariable(module, expressions.identifier);
      if (v == null) {
        result.type = StatementResultType.ERROR_RUNTIME_RESULT;
        result.error =
            "[${expressions.rowNum}:${expressions.colNum}]:${expressions.identifier}未定义";
      } else {
        result.returnvalue = v.value;
      }
      break;
    case ExpressionType.COMPLEX_EXPRESSION:
      Value t1;

      ///排除0i的情况
      if ((expressions.intvalue ?? expressions.doublevalue) == 0) {
        t1 = Value(ValueType.INT_VALUE, 0);
      } else {
        t1 = Value(ValueType.COMPLEX_VALUE,
            Complex(0, (expressions.intvalue ?? expressions.doublevalue)!));
      }
      result.returnvalue = t1;
      break;
    case ExpressionType.COMMA_EXPRESSION:
      result = expressionStatement(module, expressions.comma.left,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        result = expressionStatement(module, expressions.comma.right,
            localEnvironment: localEnvironment);
      }
      break;
    case ExpressionType.ASSIGN_EXPRESSION:

      ///=为增加变量，其余为变量操作
      result = expressionStatement(module, expressions.assignExpression.operand,
          localEnvironment: localEnvironment);

      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        if ((expressions.assignExpression.left.type ==
                ExpressionType.IDENTIFIER_EXPRESSION) ||
            (expressions.assignExpression.left.type ==
                ExpressionType.GLOBAL_EXPRESSION) ||
            (expressions.assignExpression.left.type ==
                ExpressionType.INDEX_EXPRESSION)) {
          ///搜索是否有同名函数定义,只搜索本模块内部
          if (expressions.assignExpression.left.type !=
              ExpressionType.INDEX_EXPRESSION) {
            String? t1 = searchFunction(
                    module, expressions.assignExpression.left.identifier)
                ?.name;
            if (t1 != null) {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error =
                  "[${expressions.rowNum}:${expressions.colNum}]:$t1和函数重名";
              return result;
            }
          }

          if (expressions.assignExpression.operator !=
              AssignmentOperator.NORMAL_ASSIGN) {
            expressions.binaryExpression = BinaryExpression(
                expressions.assignExpression.left,
                expressions.assignExpression.operand);
            switch (expressions.assignExpression.operator) {
              case AssignmentOperator.ADD_ASSIGN:
                expressions.type = ExpressionType.ADD_EXPRESSION;
                break;
              case AssignmentOperator.SUB_ASSIGN:
                expressions.type = ExpressionType.SUB_EXPRESSION;
                break;
              case AssignmentOperator.MUL_ASSIGN:
                expressions.type = ExpressionType.MUL_EXPRESSION;
                break;
              case AssignmentOperator.DIV_ASSIGN:
                expressions.type = ExpressionType.DIV_EXPRESSION;
                break;
              case AssignmentOperator.MOD_ASSIGN:
                expressions.type = ExpressionType.MOD_EXPRESSION;
                break;
              case AssignmentOperator.BIT_XOR_ASSIGN:
                expressions.type = ExpressionType.BIT_XOR_EXPRESSION;
                break;
              case AssignmentOperator.BIT_AND_ASSIGN:
                expressions.type = ExpressionType.BIT_AND_EXPRESSION;
                break;
              case AssignmentOperator.BIT_OR_ASSIGN:
                expressions.type = ExpressionType.BIT_OR_EXPRESSION;
                break;
              case AssignmentOperator.NORMAL_ASSIGN:
                expressions.type = ExpressionType.ASSIGN_EXPRESSION;
                break;
            }
            result = expressionStatement(module, expressions,
                localEnvironment: localEnvironment);
          }
          if (result.type != StatementResultType.ERROR_RUNTIME_RESULT) {
            ///数组表达式在左边只允许修改，不许增加
            if (expressions.assignExpression.left.type ==
                ExpressionType.INDEX_EXPRESSION) {
              Value t2 = result.returnvalue;
              result = expressionStatement(module,
                  expressions.assignExpression.left.indexExpression.array,
                  localEnvironment: localEnvironment);
              if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
                ArrayValue t1 = result.returnvalue.value;
                StatementResult result1 = StatementResult();
                result1.returnvalue = Value(ValueType.INT_VALUE, 0);
                if (expressions
                        .assignExpression.left.indexExpression.index.type ==
                    ExpressionType.COMMA_EXPRESSION) {
                  result = expressionStatement(
                      module,
                      expressions.assignExpression.left.indexExpression.index
                          .comma.left,
                      localEnvironment: localEnvironment);
                  result1 = expressionStatement(
                      module,
                      expressions.assignExpression.left.indexExpression.index
                          .comma.right,
                      localEnvironment: localEnvironment);
                } else {
                  result = expressionStatement(module,
                      expressions.assignExpression.left.indexExpression.index,
                      localEnvironment: localEnvironment);
                }
                if (result.type != StatementResultType.ERROR_RUNTIME_RESULT &&
                    result1.type != StatementResultType.ERROR_RUNTIME_RESULT) {
                  if (result.returnvalue.type == ValueType.INT_VALUE &&
                      result1.returnvalue.type == ValueType.INT_VALUE) {
                    t1.setValue(result.returnvalue.value, t2,
                        l: result1.returnvalue.value);
                    return result..returnvalue = t2;
                  } else {
                    result.type = StatementResultType.ERROR_RUNTIME_RESULT;
                    result.error =
                        "[${expressions.rowNum}:${expressions.colNum}]:数组必须用整数标";
                  }
                }
              } else {
                result.type = StatementResultType.ERROR_RUNTIME_RESULT;
                result.error =
                    "[${expressions.rowNum}:${expressions.colNum}]:非数组类型";
              }
            } else if (expressions.assignExpression.left.type ==
                ExpressionType.GLOBAL_EXPRESSION) {
              v = searchGlobalVariable(
                  module, expressions.assignExpression.left.identifier);
              if (v == null) {
                result.type = StatementResultType.ERROR_RUNTIME_RESULT;
                result.error =
                    "${expressions.assignExpression.left.identifier}不存在";
              } else {
                if (!v.isFixed) {
                  v.value = result.returnvalue;
                } else {
                  print("不可修改常量");
                }
              }
            } else {
              searchAndAddVariable(localEnvironment, module,
                  expressions.assignExpression.left, result.returnvalue);
            }
          }
        } else if (expressions.assignExpression.left.type ==
            ExpressionType.MODULE_CALL_EXPRESSION) {
          ///如果是模块调用表达式,不允许给另一个模块增加变量
          String? t1 = Interpreter.getInstance()
              .module[module]
              ?.imp[expressions.assignExpression.left.module];
          if (t1 != null) {
            if (!Interpreter.getInstance().module.containsKey(t1)) {
              ///模块还未编译
              loadModule(t1);
            }
            if (expressions.assignExpression.operator !=
                AssignmentOperator.NORMAL_ASSIGN) {
              expressions.binaryExpression = BinaryExpression(
                  expressions.assignExpression.left,
                  expressions.assignExpression.operand)
                ..left.module = null
                ..left.type = ExpressionType.IDENTIFIER_EXPRESSION;
              switch (expressions.assignExpression.operator) {
                case AssignmentOperator.ADD_ASSIGN:
                  expressions.type = ExpressionType.ADD_EXPRESSION;
                  break;
                case AssignmentOperator.SUB_ASSIGN:
                  expressions.type = ExpressionType.SUB_EXPRESSION;
                  break;
                case AssignmentOperator.MUL_ASSIGN:
                  expressions.type = ExpressionType.MUL_EXPRESSION;
                  break;
                case AssignmentOperator.DIV_ASSIGN:
                  expressions.type = ExpressionType.DIV_EXPRESSION;
                  break;
                case AssignmentOperator.MOD_ASSIGN:
                  expressions.type = ExpressionType.MOD_EXPRESSION;
                  break;
                case AssignmentOperator.BIT_XOR_ASSIGN:
                  expressions.type = ExpressionType.BIT_XOR_EXPRESSION;
                  break;
                case AssignmentOperator.BIT_AND_ASSIGN:
                  expressions.type = ExpressionType.BIT_AND_EXPRESSION;
                  break;
                case AssignmentOperator.BIT_OR_ASSIGN:
                  expressions.type = ExpressionType.BIT_OR_EXPRESSION;
                  break;
                case AssignmentOperator.NORMAL_ASSIGN:
                  expressions.type = ExpressionType.ASSIGN_EXPRESSION;
                  break;
              }
              result = expressionStatement(t1, expressions,
                  localEnvironment: localEnvironment);
            }
            v = searchGlobalVariable(
                t1, expressions.assignExpression.left.identifier);
            if (v == null) {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error =
                  "${expressions.assignExpression.left.identifier}变量不存在";
            } else {
              if (!v.isFixed) {
                v.value = result.returnvalue;
              } else {
                print("不可修改常量");
              }
            }
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error =
                "[${expressions.rowNum}:${expressions.colNum}]:${expressions.assignExpression.left.module}模块不存在";
          }
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error =
              "[${expressions.rowNum}:${expressions.colNum}]:只能给变量赋值";
        }
      }
      break;
    case ExpressionType.ADD_EXPRESSION:
    case ExpressionType.SUB_EXPRESSION:
    case ExpressionType.MUL_EXPRESSION:
    case ExpressionType.DIV_EXPRESSION:
    case ExpressionType.MOD_EXPRESSION:
    case ExpressionType.DOT_DIV_EXPRESSION:
    case ExpressionType.DOT_MUL_EXPRESSION:
    case ExpressionType.BIT_AND_EXPRESSION:
    case ExpressionType.BIT_OR_EXPRESSION:
    case ExpressionType.BIT_XOR_EXPRESSION:
    case ExpressionType.EQ_EXPRESSION:
    case ExpressionType.NE_EXPRESSION:
    case ExpressionType.GT_EXPRESSION:
    case ExpressionType.GE_EXPRESSION:
    case ExpressionType.LT_EXPRESSION:
    case ExpressionType.LE_EXPRESSION:
      result = evalBinaryExpression(module, expressions,
          localEnvironment: localEnvironment);
      break;
    case ExpressionType.LOGICAL_AND_EXPRESSION:
    case ExpressionType.LOGICAL_OR_EXPRESSION:
      result = expressionStatement(module, expressions.binaryExpression.left,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        if (result.returnvalue.type == ValueType.BOOLEAN_VALUE) {
          if (expressions.type == ExpressionType.LOGICAL_OR_EXPRESSION) {
            if (!result.returnvalue.value) {
              result = expressionStatement(
                  module, expressions.binaryExpression.right,
                  localEnvironment: localEnvironment);
              if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
                if (result.returnvalue.type == ValueType.BOOLEAN_VALUE) {
                  return result;
                } else {
                  result.type = StatementResultType.ERROR_RUNTIME_RESULT;
                  result.error = "非bool表达式";
                }
              }
            }
          } else {
            if (result.returnvalue.value) {
              result = expressionStatement(
                  module, expressions.binaryExpression.right,
                  localEnvironment: localEnvironment);
              if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
                if (result.returnvalue.type == ValueType.BOOLEAN_VALUE) {
                  return result;
                } else {
                  result.type = StatementResultType.ERROR_RUNTIME_RESULT;
                  result.error = "非bool表达式";
                }
              }
            }
          }
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "非bool表达式";
        }
      }
      break;
    case ExpressionType.MINUS_EXPRESSION:
      expressions.type = ExpressionType.SUB_EXPRESSION;
      expressions.binaryExpression = BinaryExpression(
          Expression(ExpressionType.INT_EXPRESSION)..intvalue = 0,
          expressions.minusExpression);
      result = expressionStatement(module, expressions,
          localEnvironment: localEnvironment);
      break;
    case ExpressionType.BIT_NOT_EXPRESSION:
      result = expressionStatement(module, expressions.minusExpression,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        if (result.returnvalue.type == ValueType.INT_VALUE) {
          result.returnvalue =
              Value(ValueType.INT_VALUE, ~result.returnvalue.value);
        } else if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
          result = arrayCacl(Value(ValueType.NULL_VALUE, null),
              result.returnvalue.value, ExpressionType.BIT_NOT_EXPRESSION);
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "非整数表达式";
        }
      }
      break;
    case ExpressionType.LOGICAL_NOT_EXPRESSION:
      result = expressionStatement(module, expressions.minusExpression,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        if (result.returnvalue.type == ValueType.BOOLEAN_VALUE) {
          result.returnvalue =
              Value(ValueType.BOOLEAN_VALUE, !result.returnvalue.value);
        } else if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
          if (result.returnvalue.value.type == MatrixType.GeneralMatrix ||
              !result.returnvalue.value.isdeterminant()) {
            result = arrayCacl(
                Value(ValueType.NULL_VALUE, null),
                result.returnvalue.value,
                ExpressionType.LOGICAL_NOT_EXPRESSION);
          } else {
            result.returnvalue = Value(ValueType.DOUBLE_VALUE, 'todo');
          }
        } else if (result.returnvalue.type == ValueType.INT_VALUE ||
            result.returnvalue.type == ValueType.DOUBLE_VALUE) {
          result.returnvalue =
              Value(result.returnvalue.type, result.returnvalue.value.abs());
        } else if (result.returnvalue.type == ValueType.COMPLEX_VALUE) {
          result.returnvalue = Value(
              ValueType.DOUBLE_VALUE, result.returnvalue.value.getModulo());
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "非bool表达式";
        }
      }
      break;
    case ExpressionType.FUNCTION_CALL_EXPRESSION:
      result = functionCallExpression(localEnvironment, module, expressions);

      ///没有return语句时默认添加null值
      break;
    case ExpressionType.NULL_EXPRESSION:
      result.returnvalue = Value(ValueType.NULL_VALUE, null);
      break;
    case ExpressionType.ARRAY_EXPRESSION:
      Value t1;
      if (expressions.array.array.isNotEmpty) {
        ArrayValue arrayValue = ArrayValue();
        for (var t1 = 0; t1 < expressions.array.array.length; t1++) {
          List<Value> value = List.empty(growable: true);
          for (Expression? t2 = expressions.array.array[t1].first;
              t2 != null;
              t2 = t2.next) {
            result = expressionStatement(module, t2,
                localEnvironment: localEnvironment);
            if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
              value.add(
                  Value(result.returnvalue.type, result.returnvalue.value));
            } else {
              return result;
            }
          }
          arrayValue.array.add(value);
        }

        t1 = Value(ValueType.ARRAY_VALUE, arrayValue);
      } else {
        t1 = Value(ValueType.ARRAY_VALUE, ArrayValue());
      }
      result.returnvalue = t1;
      break;
    case ExpressionType.INDEX_EXPRESSION:

      ///先计算表达式，找到对应的数组,如果是其他模块的
      result = expressionStatement(module, expressions.indexExpression.array,
          localEnvironment: localEnvironment);
      if (result.type != StatementResultType.ERROR_RUNTIME_RESULT) {
        if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
          ArrayValue t1 = result.returnvalue.value;
          StatementResult result1 = StatementResult();
          result1.returnvalue = Value(ValueType.INT_VALUE, 0);
          if (expressions.indexExpression.index.type ==
              ExpressionType.COMMA_EXPRESSION) {
            result = expressionStatement(
                module, expressions.indexExpression.index.comma.left,
                localEnvironment: localEnvironment);
            result1 = expressionStatement(
                module, expressions.indexExpression.index.comma.right,
                localEnvironment: localEnvironment);
          } else {
            result = expressionStatement(
                module, expressions.indexExpression.index,
                localEnvironment: localEnvironment);
          }
          if (result.type != StatementResultType.ERROR_RUNTIME_RESULT &&
              result1.type != StatementResultType.ERROR_RUNTIME_RESULT) {
            if (result.returnvalue.type == ValueType.INT_VALUE &&
                result1.returnvalue.type == ValueType.INT_VALUE) {
              result.returnvalue = t1.getValue(result.returnvalue.value,
                  l: result1.returnvalue.value);
            } else {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error =
                  "[${expressions.rowNum}:${expressions.colNum}]:数组必须用整数标";
            }
          }
        } else {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "[${expressions.rowNum}:${expressions.colNum}]:非数组类型";
        }
      }
      break;
    case ExpressionType.INCREMENT_EXPRESSION:
      result = expressionStatement(module, expressions.incDec.operand,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        expressions.type = ExpressionType.ASSIGN_EXPRESSION;
        expressions.assignExpression =
            AssignExpression(AssignmentOperator.NORMAL_ASSIGN)
              ..left = expressions.incDec.operand
              ..operand = (Expression(ExpressionType.ADD_EXPRESSION)
                ..binaryExpression = (BinaryExpression(
                    expressions.incDec.operand,
                    Expression(ExpressionType.INT_EXPRESSION)..intvalue = 1)));

        result = expressionStatement(module, expressions,
            localEnvironment: localEnvironment);
      }
      break;
    case ExpressionType.DECREMENT_EXPRESSION:
      result = expressionStatement(module, expressions.incDec.operand,
          localEnvironment: localEnvironment);
      if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
        expressions.type = ExpressionType.ASSIGN_EXPRESSION;
        expressions.assignExpression =
            AssignExpression(AssignmentOperator.NORMAL_ASSIGN)
              ..left = expressions.incDec.operand
              ..operand = (Expression(ExpressionType.SUB_EXPRESSION)
                ..binaryExpression = (BinaryExpression(
                    expressions.incDec.operand,
                    (Expression(ExpressionType.INT_EXPRESSION)
                      ..intvalue = 1))));
        result = expressionStatement(module, expressions,
            localEnvironment: localEnvironment);
      }
      break;
    case ExpressionType.MODULE_CALL_EXPRESSION:
      expressions.type = ExpressionType.IDENTIFIER_EXPRESSION;
      if (!Interpreter.getInstance()
          .module[module]!
          .imp
          .containsKey(expressions.module)) {
        result.type = StatementResultType.ERROR_RUNTIME_RESULT;
        result.error = "${expressions.module}模块未导入";
        return result;
      }

      String package =
          Interpreter.getInstance().module[module]!.imp[expressions.module]!;
      if (!Interpreter.getInstance().module.containsKey(package)) {
        ///模块还未编译
        loadModule(package);
      }
      result = expressionStatement(package, expressions,
          localEnvironment: localEnvironment);
      expressions.type = ExpressionType.MODULE_CALL_EXPRESSION;
      break;
    case ExpressionType.Invalid_EXPRESSION:
    case ExpressionType.EXPRESSION_TYPE_COUNT_PLUS_1:
      result.type = StatementResultType.STATEMENT_RESULT_TYPE_COUNT_PLUS_1;
      result.error = "表达式类型错误";
      break;
  }
  return result;
}

StatementResult constStatement(String module, ConstStatement consts,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  for (IdentifierList? t1 = consts.identifierList.first;
      t1 != null;
      t1 = t1.next) {
    var temp1 = t1.expression.assignExpression;
    result = expressionStatement(module, temp1.operand,
        localEnvironment: localEnvironment);
    if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
      return result;
    }
    String? t2 = searchFunction(module, temp1.left.identifier)?.name;
    if (t2 != null) {
      result.type = StatementResultType.ERROR_RUNTIME_RESULT;
      result.error =
          "[${temp1.left.rowNum}:${temp1.left.colNum}]:const定义:$t2和函数重名";
      return result;
    }

    if (localEnvironment == null) {
      for (var entry in Interpreter.getInstance().module[module]!.variable) {
        if (entry.name == temp1.left.identifier) {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error =
              "[${t1.expression.rowNum}:${t1.expression.colNum}]:const定义:${entry.name}已定义";
          return result;
        }
      }

      Interpreter.getInstance().module[module]!.variable.add(
          Variable(temp1.left.identifier, result.returnvalue, isFixed: true));
    } else {
      for (var entry in localEnvironment) {
        if (entry.name == temp1.left.identifier) {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error =
              "[${temp1.left.rowNum}:${temp1.left.colNum}]:const定义:$t2局部已定义";
          return result;
        }
      }
      localEnvironment.add(
          Variable(temp1.left.identifier, result.returnvalue, isFixed: true));
    }
  }
  return result;
}

StatementResult blockStatement(String module, Block blocks,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  LinkedList<Variable> mlocalEnvironment = LinkedList();
  if (localEnvironment != null) {
    ///块区域除了有本地变量,还可以访问上层变量
    for (var entry in localEnvironment) {
      mlocalEnvironment
          .add(Variable(entry.name, entry.value, isFixed: entry.isFixed));
    }
  }
  result = interpreterRun(module, blocks.statementList,
      localEnvironment: mlocalEnvironment);

  localEnvironment?.forEach((entry) {
    if (entry.isFixed) return;

    for (var e in mlocalEnvironment) {
      if (entry.name == e.name) {
        entry.value = e.value;
      }
    }
  });

  return result;
}

StatementResult ifStatement(String module, IfStatement ifs,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();

  result = expressionStatement(module, ifs.condition,
      localEnvironment: localEnvironment);
  if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
    return result;
  }

  if (result.returnvalue.type == ValueType.BOOLEAN_VALUE) {
    if (result.returnvalue.value) {
      result = blockStatement(module, ifs.thenblock,
          localEnvironment: localEnvironment);
    } else {
      if (ifs.elsifList.isNotEmpty) {
        for (Elsif? t1 = ifs.elsifList.first; t1 != null; t1 = t1.next) {
          result = expressionStatement(module, t1.condition,
              localEnvironment: localEnvironment);
          if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
            return result;
          }
          if (result.returnvalue.type != ValueType.BOOLEAN_VALUE) {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error =
                "[${t1.condition.rowNum}:${t1.condition.colNum}]:if条件语句不是bool型";
            return result;
          }
          if (result.returnvalue.value) {
            if (blockStatement(module, t1.block,
                        localEnvironment: localEnvironment)
                    .type ==
                StatementResultType.ERROR_RUNTIME_RESULT) {
              return result;
            }
            break;
          }
        }
      }
      if (!result.returnvalue.value) {
        result = blockStatement(module, ifs.elseblock,
            localEnvironment: localEnvironment);
      }
    }
  } else {
    result.type = StatementResultType.ERROR_RUNTIME_RESULT;
    result.error =
        "[${ifs.condition.rowNum}:${ifs.condition.colNum}]:if条件语句不是bool型";
  }
  return result;
}

StatementResult whileStatement(String module, WhileStatement whiles,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();

  for (;;) {
    result = expressionStatement(module, whiles.condition,
        localEnvironment: localEnvironment);
    if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
      return result;
    }
    if (result.returnvalue.type != ValueType.BOOLEAN_VALUE) {
      result.type = StatementResultType.ERROR_RUNTIME_RESULT;
      result.error =
          "[${whiles.condition.rowNum}:${whiles.condition.colNum}]:while条件不是bool型";
      return result;
    }
    if (!result.returnvalue.value) {
      break;
    }
    result = blockStatement(module, whiles.block,
        localEnvironment: localEnvironment);
    if (result.type == StatementResultType.RETURN_STATEMENT_RESULT ||
        result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
      break;
    } else if (result.type == StatementResultType.BREAK_STATEMENT_RESULT) {
      result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
      break;
    }
  }
  return result;
}

StatementResult forStatement(String module, ForStatement fors,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();

  if (fors.init.type != ExpressionType.Invalid_EXPRESSION) {
    result = expressionStatement(module, fors.init,
        localEnvironment: localEnvironment);
  }

  if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
    for (;;) {
      if (fors.condition.type != ExpressionType.Invalid_EXPRESSION) {
        result = expressionStatement(module, fors.condition,
            localEnvironment: localEnvironment);
        if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
          break;
        } else if (result.returnvalue.type != ValueType.BOOLEAN_VALUE) {
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error =
              "[${fors.condition.rowNum}:${fors.condition.colNum}]:for条件不是bool型";
          return result;
        } else if (!result.returnvalue.value) {
          break;
        }
      }
      result = blockStatement(module, fors.block,
          localEnvironment: localEnvironment);

      if (result.type == StatementResultType.RETURN_STATEMENT_RESULT) {
        break;
      } else if (result.type == StatementResultType.BREAK_STATEMENT_RESULT) {
        result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
        break;
      } else if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
        break;
      }
      if (fors.post.type != ExpressionType.Invalid_EXPRESSION) {
        result = expressionStatement(module, fors.post,
            localEnvironment: localEnvironment);
        if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
          break;
        }
      }
    }
  }

  return result;
}

StatementResult returnStatement(String module, ReturnStatement returns,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  if (returns.returnvalue.type != ExpressionType.Invalid_EXPRESSION) {
    result = expressionStatement(module, returns.returnvalue,
        localEnvironment: localEnvironment);
  } else {
    result.returnvalue = Value(ValueType.NULL_VALUE, null);
  }
  if (result.type != StatementResultType.ERROR_RUNTIME_RESULT) {
    result.type = StatementResultType.RETURN_STATEMENT_RESULT;
  }
  return result;
}

StatementResult switchStatement(String module, SwitchStatement switchs,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = expressionStatement(module, switchs.expression,
      localEnvironment: localEnvironment);
  if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
    StatementResult? result1;
    for (CaseList? t1 = switchs.caseList.first; t1 != null; t1 = t1.next) {
      for (Expression? t2 = t1.expressionList.first; t2 != null; t2 = t2.next) {
        result1 =
            expressionStatement(module, t2, localEnvironment: localEnvironment);
        if (result.returnvalue == result1.returnvalue) {
          result = blockStatement(module, t1.block,
              localEnvironment: localEnvironment);
          return result;
        }
      }
    }

    result = blockStatement(module, switchs.defaultblock,
        localEnvironment: localEnvironment);
  }
  return result;
}

StatementResult tryStatement(String module, TryStatement trys,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();

  result =
      blockStatement(module, trys.tryblock, localEnvironment: localEnvironment);
  if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
    result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
    var t1 = Variable(
        trys.exception, Value(ValueType.STRING_VALUE, result.error),
        isFixed: true);

    LinkedList<Variable> mlocalEnvironment = LinkedList();

    if (localEnvironment != null) {
      if (searchLocalVariable(localEnvironment, trys.exception) != null) {
        result.type = StatementResultType.ERROR_RUNTIME_RESULT;
        result.error = "catch变量重定义";
        return result;
      }
      localEnvironment.add(t1);
    } else {
      mlocalEnvironment.add(t1);
    }
    result = blockStatement(module, trys.catchblock,
        localEnvironment: localEnvironment ?? mlocalEnvironment);
    localEnvironment?.remove(t1);
  }

  if (result.type == StatementResultType.NORMAL_STATEMENT_RESULT) {
    result = blockStatement(module, trys.finallyblock,
        localEnvironment: localEnvironment);
  }
  return result;
}

StatementResult evalBinaryExpression(String module, Expression expression,
    {LinkedList<Variable>? localEnvironment}) {
  StatementResult result = StatementResult();
  result = expressionStatement(module, expression.binaryExpression.left,
      localEnvironment: localEnvironment);
  if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) return result;
  Value leftVal = result.returnvalue;
  result = expressionStatement(module, expression.binaryExpression.right,
      localEnvironment: localEnvironment);
  if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) return result;
  Value rightVal = result.returnvalue;

  ///1.判断是否为== !=
  if (expression.type == ExpressionType.EQ_EXPRESSION) {
    result.returnvalue =
        Value(ValueType.BOOLEAN_VALUE, leftVal.value == rightVal.value);

    return result;
  } else if (expression.type == ExpressionType.NE_EXPRESSION) {
    result.returnvalue =
        Value(ValueType.BOOLEAN_VALUE, leftVal.value != rightVal.value);
    return result;
  }

  ///2.判断是否为比较表达式
  ///根据左边类型进行判断
  result = binaryExpression(leftVal, rightVal, expression.type);

  if (result.type == StatementResultType.ERROR_RUNTIME_RESULT) {
    result.error =
        "[${expression.rowNum ?? expression.binaryExpression.left.rowNum}:${expression.colNum ?? expression.binaryExpression.left.colNum}]:${result.error}";
    // result.error =
    //     "[${expression.binaryExpression.left.rowNum}:${expression.binaryExpression.left.colNum}]:${result.error}";
  }
  return result;
}

StatementResult binaryExpression(
    Value leftval, Value rightval, ExpressionType expression) {
  StatementResult result = StatementResult();
  switch (leftval.type) {
    case ValueType.INT_VALUE:
    case ValueType.DOUBLE_VALUE:
      switch (expression) {
        case ExpressionType.ADD_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(
                (leftval.type == ValueType.DOUBLE_VALUE) ||
                        (rightval.type == ValueType.DOUBLE_VALUE)
                    ? ValueType.DOUBLE_VALUE
                    : ValueType.INT_VALUE,
                leftval.value + rightval.value);
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(rightval.value.a, rightval.value.b);
            t1.a += leftval.value;
            result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.ADD_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.SUB_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(
                (leftval.type == ValueType.DOUBLE_VALUE) ||
                        (rightval.type == ValueType.DOUBLE_VALUE)
                    ? ValueType.DOUBLE_VALUE
                    : ValueType.INT_VALUE,
                leftval.value - rightval.value);
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(rightval.value.a, rightval.value.b);
            t1.a = leftval.value - t1.a;
            t1.b = -t1.b;
            result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.SUB_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.MUL_EXPRESSION:
          if ((leftval.value == 0) || (rightval.value == 0)) {
            result.returnvalue = Value(ValueType.INT_VALUE, 0);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            if ((leftval.type == ValueType.DOUBLE_VALUE) ||
                (rightval.type == ValueType.DOUBLE_VALUE)) {
              result.returnvalue = Value(ValueType.DOUBLE_VALUE,
                  NumUtil.multiply(leftval.value, rightval.value));
            } else {
              result.returnvalue = Value(ValueType.INT_VALUE,
                  NumUtil.multiply(leftval.value, rightval.value).toInt());
            }
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(rightval.value.a, rightval.value.b);
            if ((leftval.type == ValueType.INT_VALUE) && (t1.a is int)) {
              t1.a = NumUtil.multiply(leftval.value, t1.a).toInt();
            } else {
              t1.a = NumUtil.multiply(leftval.value, t1.a);
            }
            if ((leftval.type == ValueType.INT_VALUE) && (t1.b is int)) {
              t1.b = NumUtil.multiply(leftval.value, t1.b).toInt();
            } else {
              t1.b = NumUtil.multiply(leftval.value, t1.b);
            }
            result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.MUL_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.DIV_EXPRESSION:
          if (leftval.value == 0) {
            result.returnvalue =
                Value(ValueType.INT_VALUE, rightval.value == 0 ? 0 / 0 : 0);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue =
                Value(ValueType.DOUBLE_VALUE, leftval.value / rightval.value);
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t1 = NumUtil.add(
                NumUtil.multiply(rightval.value.a, rightval.value.a),
                NumUtil.multiply(rightval.value.b, rightval.value.b));
            var t2 = NumUtil.divide(
                NumUtil.multiply(rightval.value.a, leftval.value), t1);
            var t3 = NumUtil.divide(
                NumUtil.multiply(-rightval.value.b, leftval.value), t1);
            result.returnvalue =
                Value(ValueType.COMPLEX_VALUE, Complex(t2, t3));
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.DIV_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.MOD_EXPRESSION:
          if (leftval.value == 0 && rightval.value != 0) {
            result.returnvalue =
                Value(ValueType.INT_VALUE, rightval.value == 0 ? 0 % 0 : 0);
          } else if (((rightval.type == ValueType.INT_VALUE) ||
                  (rightval.type == ValueType.DOUBLE_VALUE)) &&
              rightval.value != 0) {
            result.returnvalue =
                Value(ValueType.DOUBLE_VALUE, leftval.value % rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.MOD_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.BIT_AND_EXPRESSION:
          if (leftval.type == ValueType.INT_VALUE) {
            if (rightval.type == ValueType.INT_VALUE) {
              result.returnvalue =
                  Value(ValueType.INT_VALUE, (leftval.value & rightval.value));
            } else if (rightval.type == ValueType.ARRAY_VALUE) {
              result = arrayCacl(
                  leftval, rightval.value, ExpressionType.BIT_AND_EXPRESSION);
            } else {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error = "表达式错误";
            }
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.BIT_OR_EXPRESSION:
          if (leftval.type == ValueType.INT_VALUE) {
            if (rightval.type == ValueType.INT_VALUE) {
              result.returnvalue =
                  Value(ValueType.INT_VALUE, (leftval.value | rightval.value));
            } else if (rightval.type == ValueType.ARRAY_VALUE) {
              result = arrayCacl(
                  leftval, rightval.value, ExpressionType.BIT_OR_EXPRESSION);
            } else {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error = "表达式错误";
            }
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.BIT_XOR_EXPRESSION:
          if (leftval.type == ValueType.INT_VALUE) {
            if (rightval.type == ValueType.INT_VALUE) {
              result.returnvalue =
                  Value(ValueType.INT_VALUE, (leftval.value ^ rightval.value));
            } else if (rightval.type == ValueType.ARRAY_VALUE) {
              result = arrayCacl(
                  leftval, rightval.value, ExpressionType.BIT_XOR_EXPRESSION);
            } else {
              result.type = StatementResultType.ERROR_RUNTIME_RESULT;
              result.error = "表达式错误";
            }
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.GT_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t2 = NumUtil.multiply(rightval.value.a, rightval.value.a) +
                NumUtil.multiply(rightval.value.b, rightval.value.b);
            result.returnvalue = Value(ValueType.BOOLEAN_VALUE,
                NumUtil.multiply(leftval.value, leftval.value) > t2);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, leftval.value > rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.GT_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.GE_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t2 = NumUtil.multiply(rightval.value.a, rightval.value.a) +
                NumUtil.multiply(rightval.value.b, rightval.value.b);
            result.returnvalue = Value(ValueType.BOOLEAN_VALUE,
                NumUtil.multiply(leftval.value, leftval.value) >= t2);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, leftval.value >= rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.GE_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.LT_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t2 = NumUtil.multiply(rightval.value.a, rightval.value.a) +
                NumUtil.multiply(rightval.value.b, rightval.value.b);
            result.returnvalue = Value(ValueType.BOOLEAN_VALUE,
                NumUtil.multiply(leftval.value, leftval.value) < t2);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, leftval.value < rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.LT_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.LE_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t2 = NumUtil.multiply(rightval.value.a, rightval.value.a) +
                NumUtil.multiply(rightval.value.b, rightval.value.b);
            result.returnvalue = Value(ValueType.BOOLEAN_VALUE,
                NumUtil.multiply(leftval.value, leftval.value) <= t2);
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, leftval.value <= rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.LE_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        default:
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "表达式错误";
          break;
      }
      break;
    case ValueType.STRING_VALUE:
      if (expression == ExpressionType.ADD_EXPRESSION) {
        result.returnvalue = Value(
            ValueType.STRING_VALUE, leftval.value + rightval.value.toString());
      } else {
        result.type = StatementResultType.ERROR_RUNTIME_RESULT;
        result.error = "字符串只能使用加法";
      }
      break;
    case ValueType.COMPLEX_VALUE:
      switch (expression) {
        case ExpressionType.ADD_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(rightval.value.a, rightval.value.b);
            t1.a += leftval.value.a;
            t1.b += leftval.value.b;
            if (t1.b == 0) {
              result.returnvalue = Value(ValueType.DOUBLE_VALUE, t1.a);
            } else {
              result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
            }
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(ValueType.COMPLEX_VALUE,
                Complex(leftval.value.a + rightval.value, leftval.value.b));
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.ADD_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.SUB_EXPRESSION:
          if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(rightval.value.a, rightval.value.b);
            t1.a = NumUtil.subtract(leftval.value.a, t1.a);
            t1.b = NumUtil.subtract(leftval.value.b, t1.b);
            if (t1.b == 0) {
              result.returnvalue = Value(ValueType.DOUBLE_VALUE, t1.a);
            } else {
              result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
            }
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(
                ValueType.COMPLEX_VALUE,
                Complex(NumUtil.subtract(leftval.value.a, rightval.value),
                    leftval.value.b));
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.SUB_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.MUL_EXPRESSION:
          if (rightval.value == 0) {
            result.returnvalue = Value(ValueType.INT_VALUE, 0);
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            Complex t1 = Complex(
                NumUtil.subtract(
                    NumUtil.multiply(leftval.value.a, rightval.value.a),
                    NumUtil.multiply(leftval.value.b, rightval.value.b)),
                NumUtil.add(NumUtil.multiply(leftval.value.a, rightval.value.b),
                    NumUtil.multiply(leftval.value.b, rightval.value.a)));
            if (t1.b == 0) {
              result.returnvalue = Value(ValueType.DOUBLE_VALUE, t1.a);
            } else {
              result.returnvalue = Value(ValueType.COMPLEX_VALUE, t1);
            }
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(
                ValueType.COMPLEX_VALUE,
                Complex(NumUtil.multiply(leftval.value.a, rightval.value),
                    NumUtil.multiply(leftval.value.b, rightval.value)));
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.MUL_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.DIV_EXPRESSION:
          if (rightval.value == 0) {
            result.returnvalue = Value(ValueType.INT_VALUE, 0 / 0);
          } else if (rightval.type == ValueType.COMPLEX_VALUE) {
            var t1 = NumUtil.add(
                NumUtil.multiply(rightval.value.a, rightval.value.a),
                NumUtil.multiply(rightval.value.b, rightval.value.b));
            var t2 = (NumUtil.add(
                    NumUtil.multiply(rightval.value.a, leftval.value.a),
                    NumUtil.multiply(rightval.value.b, leftval.value.b)) /
                t1);
            var t3 = (NumUtil.subtract(
                    NumUtil.multiply(rightval.value.a, leftval.value.b),
                    NumUtil.multiply(rightval.value.b, leftval.value.a)) /
                t1);

            if (t3 == 0) {
              result.returnvalue = Value(ValueType.DOUBLE_VALUE, t2);
            } else {
              result.returnvalue =
                  Value(ValueType.COMPLEX_VALUE, Complex(t2, t3));
            }
          } else if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE)) {
            result.returnvalue = Value(
                ValueType.COMPLEX_VALUE,
                Complex(leftval.value.a / rightval.value,
                    leftval.value.b / rightval.value));
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.DIV_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.GT_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE) ||
              (rightval.type == ValueType.COMPLEX_VALUE)) {
            Complex t1 = leftval.value;
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, t1 > rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.GT_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.GE_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE) ||
              (rightval.type == ValueType.COMPLEX_VALUE)) {
            Complex t1 = leftval.value;
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, t1 >= rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.GE_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.LT_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE) ||
              (rightval.type == ValueType.COMPLEX_VALUE)) {
            Complex t1 = leftval.value;
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, t1 < rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.LT_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        case ExpressionType.LE_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE) ||
              (rightval.type == ValueType.COMPLEX_VALUE)) {
            Complex t1 = leftval.value;
            result.returnvalue =
                Value(ValueType.BOOLEAN_VALUE, t1 <= rightval.value);
          } else if (rightval.type == ValueType.ARRAY_VALUE) {
            result = arrayCacl(
                leftval, rightval.value, ExpressionType.LE_EXPRESSION);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        default:
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "运算符错误";
          break;
      }
      break;
    case ValueType.ARRAY_VALUE:
      switch (expression) {
        case ExpressionType.ADD_EXPRESSION:
        case ExpressionType.SUB_EXPRESSION:
        case ExpressionType.MUL_EXPRESSION:
        case ExpressionType.DIV_EXPRESSION:
        case ExpressionType.BIT_AND_EXPRESSION:
        case ExpressionType.BIT_OR_EXPRESSION:
        case ExpressionType.BIT_XOR_EXPRESSION:
        case ExpressionType.GT_EXPRESSION:
        case ExpressionType.GE_EXPRESSION:
        case ExpressionType.LT_EXPRESSION:
        case ExpressionType.LE_EXPRESSION:
        case ExpressionType.MOD_EXPRESSION:
          if ((rightval.type == ValueType.INT_VALUE) ||
              (rightval.type == ValueType.DOUBLE_VALUE) ||
              (rightval.type == ValueType.COMPLEX_VALUE)) {
            result = arrayCaclR(leftval.value, rightval, expression);
          } else {
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
          }
          break;
        default:
          result.type = StatementResultType.ERROR_RUNTIME_RESULT;
          result.error = "表达式错误";
          break;
      }
      if (rightval.type == ValueType.ARRAY_VALUE) {
        switch (expression) {
          case ExpressionType.ADD_EXPRESSION:
            result.returnvalue = leftval.value + rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.SUB_EXPRESSION:
            result.returnvalue = leftval.value - rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.MUL_EXPRESSION:
            break;
          case ExpressionType.DIV_EXPRESSION:
            break;
          case ExpressionType.BIT_AND_EXPRESSION:
            result.returnvalue = leftval.value & rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.BIT_OR_EXPRESSION:
            result.returnvalue = leftval.value | rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.BIT_XOR_EXPRESSION:
            result.returnvalue = leftval.value ^ rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.GT_EXPRESSION:
            result.returnvalue = leftval.value > rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.GE_EXPRESSION:
            result.returnvalue = leftval.value >= rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.LT_EXPRESSION:
            result.returnvalue = leftval.value < rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.LE_EXPRESSION:
            result.returnvalue = leftval.value <= rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.MOD_EXPRESSION:
            result.returnvalue = leftval.value % rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.DOT_MUL_EXPRESSION:

            ///对应乘法
            result.returnvalue = leftval.value * rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          case ExpressionType.DOT_DIV_EXPRESSION:
            result.returnvalue = leftval.value / rightval.value;
            if (result.returnvalue.type == ValueType.ARRAY_VALUE) {
              result.type = StatementResultType.NORMAL_STATEMENT_RESULT;
            }
            break;
          default:
            result.type = StatementResultType.ERROR_RUNTIME_RESULT;
            result.error = "表达式错误";
            break;
        }
      }
      break;
    case ValueType.NULL_VALUE:
    case ValueType.REGEXP_VALUE:
    case ValueType.BOOLEAN_VALUE:
    case ValueType.FUNCTION_VALUE:
    case ValueType.NATIVE_POINTER_VALUE:
    case ValueType.UNDEFINED_VALUE:
      result.type = StatementResultType.ERROR_RUNTIME_RESULT;
      result.error = "运算符错误";
      break;
  }
  return result;
}
